package com.mobile.learning;

import java.util.Date;
import java.text.SimpleDateFormat;

import com.markupartist.android.widget.ActionBar;
import com.markupartist.android.widget.ActionBar.IntentAction;
import com.mobile.learning.lib.DBClass;
import com.mobile.learning.lib.Nilai;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.Chronometer;
import android.widget.TextView;

public class NilaiActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_nilai);

		ActionBar actionBar = (ActionBar) findViewById(R.id.actionbar_post);
		actionBar.setTitle("NILAI ANDA !");

		actionBar.setHomeAction(new IntentAction(this, MainMenuActivity
				.createIntent(this), R.drawable.ic_action_back));
		Bundle extras = getIntent().getExtras();
		int score = extras.getInt("score");
		long elTime = extras.getLong("elTime");
		int id_jenis = extras.getInt("jenis");
		TextView nilai = (TextView) findViewById(R.id.t_nilai);
		Chronometer ctime = (Chronometer) findViewById(R.id.c_time);
		nilai.setText(String.valueOf(score));
		int stopedMillisecond = 0;
		
		
		String array[] = ctime.getText().toString().split(":");
		stopedMillisecond = Integer.parseInt(array[0]) * 60 * 1000
				+ Integer.parseInt(array[1]) * 1000;
		ctime.setBase(elTime - stopedMillisecond);
		ctime.stop();
		
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
		String currentDateandTime = sdf.format(new Date());
		
		DBClass db = new DBClass(this);
		db.insertNilai(new Nilai(currentDateandTime, id_jenis, score));
		

	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if ((keyCode == KeyEvent.KEYCODE_BACK)) {
			this.finish();
			Intent intent;
			intent = new Intent(this, MainMenuActivity.class);
			startActivity(intent);
		}
		return super.onKeyDown(keyCode, event);
	}
}
